/*
*Copyright (c) Nikhil Jain.
*Project ICARUS, communications and 
*artificial intelligence.
*/



package javadrone.client.simulator;

//~--- non-JDK imports --------------------------------------------------------

import simbad.sim.Simulator;

//~--- JDK imports ------------------------------------------------------------

import java.awt.Container;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

/**
 * The GUI panel for controlling the simulator.
 */
public class DisabledSimulatorControlGUI extends JPanel implements ActionListener {
    private static final long serialVersionUID = 1L;
    JFrame                    parentFrame;
    Simulator                 simulator;
    Font                      smallFont;
    JFormattedTextField       timeFactorField;

    public DisabledSimulatorControlGUI(JFrame parentFrame, Simulator simulator) {
        this.simulator = simulator;
        smallFont      = new Font("Arial", Font.PLAIN, 11);
        setFont(smallFont);
        createGUI();
        this.parentFrame = parentFrame;
    }

    public JMenu createMenu() {
        JMenu menu = new JMenu("Simulator");

        // TODO
        return menu;
    }

    void createGUI() {
        setBorder(BorderFactory.createCompoundBorder(BorderFactory.createTitledBorder("Simulator"),
                BorderFactory.createEmptyBorder(5, 5, 5, 5)));
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

        // Control buttons
        JPanel panel1 = new JPanel();

        panel1.setLayout(new BoxLayout(panel1, BoxLayout.X_AXIS));
        createButton(panel1, "run", "run");
        createButton(panel1, "pause", "pause");
        createButton(panel1, "reset", "reset");
        createButton(panel1, "restart", "restart");
        createButton(panel1, "step", "step1");
        add(panel1);

        // time factor buttons
        JPanel panel2 = new JPanel();

        panel2.setLayout(new BoxLayout(panel2, BoxLayout.X_AXIS));
        createLabel(panel2, "Time Factor");

        ButtonGroup bgroup = new ButtonGroup();

        createRadioButton(panel2, bgroup, "0.2", "tf0.2", false);
        createRadioButton(panel2, bgroup, "0.5", "tf0.5", false);
        createRadioButton(panel2, bgroup, "1.0", "tf1.0", true);
        createRadioButton(panel2, bgroup, "5.0", "tf5.0", false);
        createRadioButton(panel2, bgroup, "10.0", "tf10.0", false);
        createRadioButton(panel2, bgroup, "20.0", "tf20.0", false);
        add(panel2);
    }

    /** helper function */
    private void createButton(Container container, String label, String action) {
        JButton b = new JButton(label);

        b.setFont(smallFont);
        b.setActionCommand(action);
        b.addActionListener(this);
        b.setEnabled(false);
        container.add(b);
    }

    /** helper function */
    private void createRadioButton(Container container, ButtonGroup group, String label, String action,
                                   boolean selected) {
        JRadioButton b = new JRadioButton(label);

        b.setActionCommand(action);
        b.setFont(smallFont);
        b.addActionListener(this);
        b.setSelected(selected);
        group.add(b);
        container.add(b);
    }

    /** helper function */
    private void createLabel(Container container, String label) {
        JLabel l = new JLabel(label);

        l.setFont(smallFont);
        container.add(l);
    }

    public void startSimulation() {
        simulator.startSimulation();
    }

    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        String action = actionEvent.getActionCommand();

        if (action.equals("run")) {
            simulator.startSimulation();
        } else if (action.equals("pause")) {
            simulator.stopSimulation();
        } else if (action.equals("reset")) {
            simulator.resetSimulation();
        } else if (action.equals("restart")) {
            simulator.restartSimulation();
        } else if (action.equals("tf0.2")) {
            simulator.setVirtualTimeFactor(0.2f);
        } else if (action.equals("tf0.5")) {
            simulator.setVirtualTimeFactor(0.5f);
        } else if (action.equals("tf1.0")) {
            simulator.setVirtualTimeFactor(1.0f);
        } else if (action.equals("tf2.0")) {
            simulator.setVirtualTimeFactor(2.0f);
        } else if (action.equals("tf5.0")) {
            simulator.setVirtualTimeFactor(5.0f);
        } else if (action.equals("tf10.0")) {
            simulator.setVirtualTimeFactor(10.0f);
        } else if (action.equals("tf20.0")) {
            simulator.setVirtualTimeFactor(20.0f);
        } else if (action.equals("tf50.0")) {
            simulator.setVirtualTimeFactor(50.0f);
        } else if (action.equals("step1")) {
            simulator.performSimulationStep();
        }
    }
}


//~ Formatted by Jindent --- http://www.jindent.com
